

#import "EBMBaseUserSingle.h"

@implementation EBMBaseUserSingle



/// 获取本地存储数据
- (void)gainUserInformationInLocal
{
    
    u_int count = 0;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    

    for (int i = 0; i < count; i++)
    {
        const char *propertyName = property_getName(properties[i]);
        NSString *attributeName = [NSString stringWithUTF8String:propertyName];
        
        SEL setMethod = [self getSetMethodWithAttName:attributeName];
        
        
        if ([self respondsToSelector:setMethod])
        {
            
            id result = [SJUserDefaultTool userDefaultGainObjectWithKey:attributeName];
            
            
            if(result && ![result isKindOfClass:[NSNull class]])
            {
                
                [self performSelector:setMethod withObject:result];
                
            }
            
        }
    }
    
    free(properties);
    
    
}

/// 数据的本地存储
- (void)userInformationInLocalStorage
{
    //  获取所有的属性
    
    u_int count = 0;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    

    for (int i = 0; i < count; i++)
    {
        const char *propertyName = property_getName(properties[i]);
        NSString *attributeName = [NSString stringWithUTF8String:propertyName];
        
        
        SEL getMethod = [self getMethodWithAttName:attributeName];
        
        if ([self respondsToSelector:getMethod])
        {
            
            id result = [self performSelector:getMethod withObject:nil];
            
            
            if(result && ![result isKindOfClass:[NSNull class]])
            {
                
                //  存储
                [SJUserDefaultTool userDefaultSetObjectWithObj:result key:attributeName];
            }
            
        }
    }
    
    free(properties);
}




/// 清楚用户信息
- (void)removeUserInfomation
{
       
    
    u_int count = 0;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    

    for (int i = 0; i < count; i++)
    {
        const char *propertyName = property_getName(properties[i]);
        NSString *attributeName = [NSString stringWithUTF8String:propertyName];
        
        SEL setMethod = [self getSetMethodWithAttName:attributeName];
        
        
        if ([self respondsToSelector:setMethod])
        {
            
            id result = [SJUserDefaultTool userDefaultGainObjectWithKey:attributeName];
            
            
            if(result && ![result isKindOfClass:[NSNull class]])
            {
                
                [self performSelector:setMethod withObject:nil];
                
                [SJUserDefaultTool userDefaultRemoveObjectWithKey:attributeName];
                
            }
            
        }
    }
    
    free(properties);

    
}





/// 拼接当前属性名字的set:方法
/// @param attName 属性名称
- (SEL)getSetMethodWithAttName:(NSString *)attName
{
    // title ->setTitle:
    // 1.获取属性名字首字母的大小
    NSString *firstName = [attName substringToIndex:1].capitalizedString;
    // 2.获取剩余的名字
    NSString *lastName = [attName substringFromIndex:1];
    // 3.拼接完整的方法名字
    NSString *setAttName = [NSString stringWithFormat:@"set%@%@:",firstName,lastName];
    return NSSelectorFromString(setAttName);
}



/// 拼接当前属性名字的get:方法
/// @param attName 属性名称
- (SEL)getMethodWithAttName:(NSString *)attName
{
    return NSSelectorFromString(attName);
}


@end
